package com.gitee.huanminabc.jtimerwheel.mode;


import com.gitee.huanminabc.jtimerwheel.base.TimeSlotInfo;
import com.gitee.huanminabc.jtimerwheel.base.TimerResolverCore;
import com.gitee.huanminabc.jtimerwheel.base.TimerWheelModeMethod;
import com.gitee.huanminabc.jtimerwheel.utils.TimerWheelDateUtil;

import java.util.Date;

/**
 * 获取以当前时间后指定年月日时分秒
 *
 * @Author: huanmin
 * @Date: 2022/11/30 19:53
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class TimePointMode implements TimerWheelModeMethod {

    @Override
    public void modeMethod(TimeSlotInfo item, int seat, TimerResolverCore timerResolver) {
        String data = item.getData();
        //解析point(?)模式,取出?的值
        int i = data.indexOf("(");
        data = data.substring( i + 1, data.length() - 1);
        Date currentDate = TimerWheelDateUtil.secondTurnDate(timerResolver.timestamp);
        if (seat == 0) { //固定秒数
            int second = Integer.parseInt(data);
            //判断当前时间是否在指定的时间点之前
            int dateTimeSecond = TimerWheelDateUtil.getDateTimeSecond(currentDate);
            if (dateTimeSecond  >=second) {
                //如果当前时间已经超过了指定的时间点,则当前时间+1分钟
                currentDate = TimerWheelDateUtil.addDate(currentDate, 0, 0, 0, 0, 1, 0);
            }
            //设置秒
            timerResolver.timestamp = TimerWheelDateUtil.setSecond(currentDate, second);
        } else if (seat == 1) { //分钟数
            int minute = Integer.parseInt(data);
            //判断当前时间是否在指定的时间点之前
            int dateTimeMinute = TimerWheelDateUtil.getDateTimeMinute(currentDate);
            if (dateTimeMinute >=minute) {
                //如果当前时间已经超过了指定的时间点,则当前时间+1小时
                currentDate = TimerWheelDateUtil.addDate(currentDate, 0, 0, 0, 1, 0, 0);
            }
            //设置分钟
            timerResolver.timestamp = TimerWheelDateUtil.setMinute(currentDate, minute);
        } else if (seat == 2) { //小时数
            int hour = Integer.parseInt(data);
            //判断当前时间是否在指定的时间点之前
            int dateTimeHour = TimerWheelDateUtil.getDateTimeHour(currentDate);
            if (dateTimeHour >=hour) {
                //如果当前时间已经超过了指定的时间点,则当前时间+1天
                currentDate = TimerWheelDateUtil.addDate(currentDate, 0, 0, 1, 0, 0, 0);
            }
            //设置小时
            timerResolver.timestamp = TimerWheelDateUtil.setHour(currentDate, hour);
        } else if (seat == 3) { //天数
            int day = Integer.parseInt(data);
            //判断当前时间是否在指定的时间点之前
            int dateTimeDay = TimerWheelDateUtil.getDateTimeDay(currentDate);
            if (dateTimeDay >=day) {
                //如果当前时间已经超过了指定的时间点,则当前时间+1天
                currentDate = TimerWheelDateUtil.addDate(currentDate, 0, 0, 1, 0, 0, 0);
            }
            //设置天数
            timerResolver.timestamp = TimerWheelDateUtil.setDay(currentDate, day);
        } else if (seat == 4) { //周n的日期
            int week = Integer.parseInt(data);
            //判断当前时间是否在指定的时间点之前
            int dateTimeWeek = TimerWheelDateUtil.getDateTimeWeek(currentDate);
            if (dateTimeWeek >=week) {
                //如果当前时间已经超过了指定的时间点,则当前时间+1周
                currentDate = TimerWheelDateUtil.addDate(currentDate, 0, 0, 7, 0, 0, 0);
            }
            //设置周数
            timerResolver.timestamp = TimerWheelDateUtil.setWeek(currentDate, week);
        } else if (seat == 5) { //月数
            int month = Integer.parseInt(data);
            //判断当前时间是否在指定的时间点之前
            int dateTimeMonth = TimerWheelDateUtil.getDateTimeMonth(currentDate);
            if (dateTimeMonth >=month) {
                //如果当前时间已经超过了指定的时间点,则当前时间+1年
                currentDate = TimerWheelDateUtil.addDate(currentDate, 1, 0, 0, 0, 0, 0);
            }
            //设置月份
            timerResolver.timestamp = TimerWheelDateUtil.setMonth(currentDate, month);
        } else if (seat == 6) { //年数
            int year = Integer.parseInt(data);
            //不能小于当前年份
            if (year < TimerWheelDateUtil.getDateTimeYear(currentDate)) {
                throw new IllegalArgumentException("year must be greater than or equal to current year");
            }
            timerResolver.timestamp =TimerWheelDateUtil.setYear(currentDate, year);
        }
    }
}
